package com.rmb.userinfo.controller;

import com.alibaba.fastjson.JSON;
import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.QueryResponseResult;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.mart.entity.userinfo.UserWarning;
import com.rmb.userinfo.service.UserWarningService;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 段段
 * @PackageName: com.rmb.userinfo.controller
 * @ClassName: UserWarningController
 * @Description: (预警号码)表控制层
 * @data: 2021/1/20 15:18
 **/
@RestController
@RequestMapping("userWarning")
public class UserWarningController {
    /**
     * 服务对象
     */
    @Resource
    private UserWarningService userWarningService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public UserWarning selectOne(Long id) {
        return this.userWarningService.queryById(id);
    }

    /**
     * 查询预警号码所有信息
     * @return
     */
    @GetMapping("/selectAll/{id}")
    public QueryResponseResult selectAll(@PathVariable("id") Long id){
        List<UserWarning> list = userWarningService.selectAll(id);
        return new QueryResponseResult(CommonCode.QUERY_SUCCESS,list);
    }

    /**
     * 查询企业下的预警号码所有信息
     * @return
     */
    @GetMapping("/findByBusinessId/{id}")
    public List<UserWarning> findByBusinessId(@PathVariable("id") Long id){
        return userWarningService.findByBusinessId(id);
    }

    /**
     * 新增数据
     *
     * @param userWarning 实例对象
     * @return 实例对象
     */
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody UserWarning userWarning){
        UserWarning insert = userWarningService.insert(userWarning);
        return insert != null ?
                new ResponseResult(CommonCode.SUCCESS, JSON.toJSONString(userWarning)) :
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("/deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Long id){
        boolean flag = userWarningService.deleteById(id);
        return flag ? new ResponseResult(CommonCode.SUCCESS) :
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 修改数据
     *
     * @param userWarning 实例对象
     * @return 实例对象
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody UserWarning userWarning){
        UserWarning update = userWarningService.update(userWarning);
        return update != null ?
                new ResponseResult(CommonCode.SUCCESS, JSON.toJSONString(userWarning)) :
                new ResponseResult(CommonCode.FAIL);
    }

}